package com.seeyon.apps.taxFund.listener;

import com.seeyon.apps.addressbook.manager.AddressBookCustomerFieldInfoManager;
import com.seeyon.apps.addressbook.po.AddressBook;
import com.seeyon.apps.commonExt.util.CommonRestUtil;
import com.seeyon.apps.taxFund.api.ShuiJinEnum;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.organization.bo.V3xOrgAccount;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.event.AddBatchMemberEvent;
import com.seeyon.ctp.organization.event.AddMemberEvent;
import com.seeyon.ctp.organization.event.DeleteMemberEvent;
import com.seeyon.ctp.organization.event.UpdateMemberEvent;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.util.annotation.ListenEvent;

import cn.hutool.core.lang.Assert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;


/**
 * @description:
 * @author: huangjin
 * @time: 2025/8/28 13:35
 */
public class OrgListener {

    private final OrgManager orgManager;

    public OrgListener(OrgManager orgManager) {
        this.orgManager = orgManager;
    }

    @ListenEvent(event = AddMemberEvent.class)
    public void addMemberEvent(AddMemberEvent event){
        V3xOrgMember member = event.getMember();
        try {
            Map<String, Object> params = convertToParams(member);
            // 用户密码
            params.put("password", "123456");
            CommonRestUtil.post(ShuiJinEnum.USER_SYNC, null, params);
        } catch (BusinessException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }

    @ListenEvent(event = AddBatchMemberEvent.class)
    public void addBatchMemberEvent(AddBatchMemberEvent event){
        List<V3xOrgMember> batchMembers = event.getBatchMembers();
        for (V3xOrgMember member : batchMembers) {
            try {
                Map<String, Object> params = convertToParams(member);
                // 用户密码
                params.put("password", "123456");
                CommonRestUtil.post(ShuiJinEnum.USER_SYNC, null, params);
            } catch (BusinessException e) {
                throw new IllegalArgumentException(e.getMessage());
            }
        }
    }

    @ListenEvent(event = UpdateMemberEvent.class)
    public void updateMemberEvent(UpdateMemberEvent event){
        V3xOrgMember member = event.getMember();
        try {
            Map<String, Object> params = convertToParams(member);
            CommonRestUtil.post(ShuiJinEnum.USER_SYNC, null, params);
        } catch (Exception e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }

    @ListenEvent(event = DeleteMemberEvent.class)
    public void deleteMemberEvent(DeleteMemberEvent event){
        V3xOrgMember member = event.getMember();
        try {
            Map<String, Object> params = convertToParams(member);
            CommonRestUtil.post(ShuiJinEnum.USER_SYNC, null, params);
        } catch (BusinessException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }

    private Map<String, Object> convertToParams(V3xOrgMember member) throws BusinessException {
        V3xOrgAccount account = orgManager.getAccountById(member.getOrgAccountId());
        Assert.notNull(account, "人员编号【{}】单位为空", member.getCode());

        String nsrsbh = Optional.ofNullable(((AddressBookCustomerFieldInfoManager) AppContext.getBean("addressBookCustomerFieldInfoManager"))
                        .getByMemberId(account.getId()))
                .map(AddressBook::getExtAttr1)
                .orElseThrow(() -> new BusinessException("人员编号【{}】税号为空", member.getCode()));

        boolean valid = member.getStatus() == 1 && member.getEnabled() && (!member.getIsDeleted()) && member.getState() == 1;

        Map<String, Object> params = new HashMap<>();
        params.put("account", member.getLoginName());

        params.put("name", member.getName());
        // 组织名称
        params.put("nsrmc", account.getName());
        // 组织税号
        params.put("nsrsbh", nsrsbh);
        params.put("status", valid ? 1 : 0);
        return params;
    }
}
